<template>
  <div>
    <form class="el-form demo-form-inline el-form--inline">
      <div class="el-form-item el-form-item--small">
        <!---->
        <div class="el-form-item__content">
          <div class="el-input el-input--small">
            <!----><input
              type="text"
              autocomplete="off"
              placeholder="商品名称"
              class="el-input__inner"
              v-model="searchs"
            /><!----><!----><!----><!---->
          </div>
          <!---->
        </div>
      </div>
      <div class="el-form-item el-form-item--small" @click="querys">
        <!---->
        <div class="el-form-item__content">
          <button
            type="button"
            class="el-button el-button--primary el-button--small"
          >
            <!----><!----><span>查询</span></button
          ><!---->
        </div>
      </div>
      <div class="el-form-item el-form-item--small">
        <!---->
        <div class="el-form-item__content">
          <button
            type="button"
            class="el-button el-button--primary el-button--small"
          >
            <!----><!----><span>添加</span></button
          ><!---->
        </div>
      </div>
      <!-- 重置按钮 -->
      <div class="el-form-item el-form-item--small" @click="resets">
        <!---->
        <div class="el-form-item__content">
          <button
            type="button"
            class="el-button el-button--primary el-button--small"
          >
            <!----><!----><span>重置</span></button
          ><!---->
        </div>
      </div>
    </form>
    <!-- 内容 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="name" label="商品名称" width="180">
      </el-table-column>
      <el-table-column prop="retail_price" label="售价"> </el-table-column>
      <el-table-column prop="goods_number" label="库存"> </el-table-column>
      <el-table-column prop="is_new" label="新品">
        <template v-slot="scope">
          {{ scope.row.is_new ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="is_hot" label="人气">
        <template v-slot="scope">
          {{ scope.row.is_hot ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="add_time" label="上架">
        <template v-slot="scope">
          {{ scope.row.add_time ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="sort_order" label="排序"> </el-table-column>
      <el-table-column label="操作">
        <template>
          <el-button class="bianji" size="mini">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 上传 -->
    <el-upload
      class="avatar-uploader"
      action=""
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="uploadGoodPic"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>

<script>
import { GetGoodApi, UploadGoodPicApi } from "@/request/httpApi.js";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1, //页码
      pageSize: 10, //一页的条数
      searchs: "", //输入框绑定
      total: 0, //总条数
      imageUrl: "", //图片地址
      headers: { "X-Nideshop-Token": localStorage.getItem("token") }, //请求头
    };
  },
  methods: {
    //   覆盖默认
    uploadGoodPic(data) {
      let formdata = new FormData();
      formdata.append("good_pic", data.file);
      UploadGoodPicApi(formdata).then((res) => {
        if (res.errno === 0) {
          this.imageUrl = res.data.fileUrl;
        }
      });
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      if (res.errno === 0) {
        this.imageUrl = res.data.fileUrl;
      }
      console.log(res);
      console.log(file);
    },
    beforeAvatarUpload(file) {},
    fetchData() {
      GetGoodApi({
        // 当前页
        page: this.currentPage,
        //   商品名称
        //   name,
        //   一页显示多少条
        size: this.pageSize,
      }).then((res) => {
        if (res.errno === 0) {
          this.tableData = res.data.data;
          this.total = res.data.count;
        }
      });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },
    handleCurrentChange(page) {
      this.totalPages = page;
      this.fetchData();
    },
    // 搜索点击事件
    querys() {
      GetGoodApi({
        // 当前页
        page: this.currentPage,
        //   商品名称
        name: this.searchs,
        //   一页显示多少条
        size: this.pageSize,
      }).then((res) => {
        if (res.errno === 0) {
          this.tableData = res.data.data;
          this.total = res.data.count;
        }
      });
    },
    // 重置点击事件
    resets() {
      this.searchs = "";
      this.fetchData();
    },
  },
  created() {
    this.fetchData();
  },
};
</script>
 
<style lang = "less" scoped>
.bianji {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
}
</style>